/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.evaluationSystem.controller;

import com.evaluationSystem.annotation.LogOperation;
import com.evaluationSystem.config.R;
import com.evaluationSystem.entity.dto.SysDeptDTO;
import com.evaluationSystem.service.SysDeptService;
import com.evaluationSystem.util.AssertUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门管理
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController {
    @Autowired
    private SysDeptService sysDeptService;

    @GetMapping("list")
    @RequiresPermissions("sys:dept:list")
    public R<List<SysDeptDTO>> list(@RequestParam Map<String, Object> params) {
        List<SysDeptDTO> list = sysDeptService.list(params);

        return R.success(list);
    }

    @GetMapping("treeList")
    @RequiresPermissions("sys:dept:list")
    public R<List<SysDeptDTO>> treeList(@RequestParam Map<String, Object> params) {
        List<SysDeptDTO> list = sysDeptService.treeList(params);

        return R.success(list);
    }

    @GetMapping("{id}")
    @RequiresPermissions("sys:dept:info")
    public R<SysDeptDTO> get(@PathVariable("id") Long id) {
        SysDeptDTO data = sysDeptService.get(id);

        return R.success(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("sys:dept:save")
    public R save(@RequestBody SysDeptDTO dto) {

        sysDeptService.save(dto);

        return R.success();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("sys:dept:update")
    public R update(@RequestBody SysDeptDTO dto) {

        sysDeptService.update(dto);

        return R.success();
    }

    @DeleteMapping("{id}")
    @LogOperation("删除")
    @RequiresPermissions("sys:dept:delete")
    public R delete(@PathVariable("id") Long id) {
        //效验数据
        AssertUtils.isNull(id, "id");

        sysDeptService.delete(id);

        return R.success();
    }

}